/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.jisuanqi;

import static java.time.Clock.system;
import sun.applet.Main;

/**
 *
 * @author Lirz
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        screen = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        ac = new javax.swing.JButton();
        fen = new javax.swing.JButton();
        delete = new javax.swing.JButton();
        dividide = new javax.swing.JButton();
        seven = new javax.swing.JButton();
        eight = new javax.swing.JButton();
        nine = new javax.swing.JButton();
        multiply = new javax.swing.JButton();
        four = new javax.swing.JButton();
        five = new javax.swing.JButton();
        six = new javax.swing.JButton();
        sub = new javax.swing.JButton();
        one = new javax.swing.JButton();
        two = new javax.swing.JButton();
        three = new javax.swing.JButton();
        add = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        zero = new javax.swing.JButton();
        dip = new javax.swing.JButton();
        getValue = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 204, 204));

        jPanel3.setBackground(new java.awt.Color(255, 153, 153));

        screen.setBackground(new java.awt.Color(255, 153, 153));
        screen.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        screen.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        screen.setText("0");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(screen)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(screen, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
        );

        jPanel1.setBackground(new java.awt.Color(255, 204, 204));
        jPanel1.setLayout(new java.awt.GridLayout(4, 4, 3, 3));

        ac.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        ac.setText("AC");
        ac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acActionPerformed(evt);
            }
        });
        jPanel1.add(ac);

        fen.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        fen.setText("+/-");
        jPanel1.add(fen);

        delete.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        delete.setText("Del");
        jPanel1.add(delete);

        dividide.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        dividide.setText("/");
        jPanel1.add(dividide);

        seven.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        seven.setText("7");
        seven.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sevenActionPerformed(evt);
            }
        });
        jPanel1.add(seven);

        eight.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        eight.setText("8");
        eight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eightActionPerformed(evt);
            }
        });
        jPanel1.add(eight);

        nine.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        nine.setText("9");
        nine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nineActionPerformed(evt);
            }
        });
        jPanel1.add(nine);

        multiply.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        multiply.setText("*");
        multiply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                multiplyActionPerformed(evt);
            }
        });
        jPanel1.add(multiply);

        four.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        four.setText("4");
        jPanel1.add(four);

        five.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        five.setText("5");
        jPanel1.add(five);

        six.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        six.setText("6");
        jPanel1.add(six);

        sub.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        sub.setText("-");
        sub.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                subActionPerformed(evt);
            }
        });
        jPanel1.add(sub);

        one.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        one.setText("1");
        one.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oneActionPerformed(evt);
            }
        });
        jPanel1.add(one);

        two.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        two.setText("2");
        jPanel1.add(two);

        three.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        three.setText("3");
        jPanel1.add(three);

        add.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        add.setText("+");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });
        jPanel1.add(add);

        jPanel2.setBackground(new java.awt.Color(255, 204, 204));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        zero.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        zero.setText("0");
        jPanel2.add(zero, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 60, 60));

        dip.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        dip.setText(".");
        jPanel2.add(dip, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 0, 60, 60));

        getValue.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        getValue.setText("=");
        jPanel2.add(getValue, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 0, 110, 60));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void acActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acActionPerformed
        // TODO add your handling code here:
        screen.setText("0");
        calculator.ac();
    }//GEN-LAST:event_acActionPerformed

    private void sevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sevenActionPerformed
        // TODO add your handling code here:
       doNum("7");
    }//GEN-LAST:event_sevenActionPerformed

    private void eightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eightActionPerformed
        // TODO add your handling code here:
       doNum("8");
    }//GEN-LAST:event_eightActionPerformed

    private void doNum(String num){
          if(newInput){
            screen.setText(0+"");
        }
        
        if(screen.getText().trim().equals("0")){
            screen.setText(num);
        }else{
            screen.setText(screen.getText()+num);
        }
        newInput = false;
    }
    
    private void nineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nineActionPerformed
        // TODO add your handling code here:
       doNum("9");
    }//GEN-LAST:event_nineActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        // TODO add your handling code here:
        doAction("+");
    }//GEN-LAST:event_addActionPerformed

    private void subActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_subActionPerformed
        // TODO add your handling code here:
          doAction("-");
    }//GEN-LAST:event_subActionPerformed

    private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_multiplyActionPerformed
        // TODO add your handling code here:
        doAction("*");
    }//GEN-LAST:event_multiplyActionPerformed

    private void oneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oneActionPerformed
        // TODO add your handling code here:
         doNum("1");
    }//GEN-LAST:event_oneActionPerformed
     private void doAction(String flag){
         
        String screenThing =  screen.getText();
        try{
            double screenNumber = Double.parseDouble(screenThing);
            if(flag.equals("+")){
            calculator.add(screenNumber);
            }else if (flag.equals("-")){
            calculator.sub(screenNumber);
            }else if (flag.equals("*")){
            calculator.multiply(screenNumber);
            }else if (flag.equals("/")){
            calculator.divided(screenNumber);
            }
            screen.setText(calculator.getvalue()+"");
            newInput = true;
        }catch(Exception e){
            e.printStackTrace();
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               new NewJFrame().setVisible(true);
            }
        });
    }

    private calculat calculator = new calculat();
   
    
    private boolean newInput = true;
    
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ac;
    private javax.swing.JButton add;
    private javax.swing.JButton delete;
    private javax.swing.JButton dip;
    private javax.swing.JButton dividide;
    private javax.swing.JButton eight;
    private javax.swing.JButton fen;
    private javax.swing.JButton five;
    private javax.swing.JButton four;
    private javax.swing.JButton getValue;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton multiply;
    private javax.swing.JButton nine;
    private javax.swing.JButton one;
    private javax.swing.JTextField screen;
    private javax.swing.JButton seven;
    private javax.swing.JButton six;
    private javax.swing.JButton sub;
    private javax.swing.JButton three;
    private javax.swing.JButton two;
    private javax.swing.JButton zero;
    // End of variables declaration//GEN-END:variables
}
